#!/bin/sh -e

# Use cmake to build cmake if installed.
# This leads to a much faster build.
if cpt-list cmake >/dev/null 2>&1; then
    cmake -B build \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_INSTALL_LIBDIR=/usr/lib \
        -DCMAKE_SHARED_LIBS=ON \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_USE_SYSTEM_BZIP2=ON \
        -DCMAKE_USE_SYSTEM_CURL=ON \
        -DCMAKE_USE_SYSTEM_EXPAT=ON \
        -DCMAKE_USE_SYSTEM_LIBARCHIVE=ON \
        -DCMAKE_USE_SYSTEM_LIBLZMA=ON \
        -DCMAKE_USE_SYSTEM_LIBUV=OFF \
        -DCMAKE_USE_SYSTEM_NGHTTP2=ON \
        -DCMAKE_USE_SYSTEM_ZLIB=ON \
        -DCMAKE_USE_SYSTEM_ZSTD=ON \
        -DBUILD_TESTING=OFF

    cmake --build   build
    DESTDIR="$1" cmake --install build

else
    ./configure \
        --prefix=/usr \
        --system-curl \
        --system-expat \
        --system-zlib \
        --system-bzip2 \
        --parallel="$(nproc)"

    make
    make DESTDIR="$1" install
fi

# This isn't the actual documentation
rm -r "$1/usr/doc"
